package com.example.cqcet_demo.activity;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.cqcet_demo.R;

public class HomeContentActivity extends AppCompatActivity{

    private WebView wv_content;
    private WebSettings webSettings;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_content);

        wv_content = (WebView) findViewById(R.id.wv_content);
        String bodyData = getIntent().getStringExtra("bodyData");
        //String bodyData = "http://www.cqcet.edu.cn/info/1034/7880.htm";
        Log.d("postData","postData="+bodyData);

        webSettings = wv_content.getSettings();

        webSettings.setJavaScriptEnabled(true);// 设置支持javascript脚本
        webSettings.setSupportZoom(true);
        // 将图片调整到合适的大小
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        wv_content.setHorizontalScrollBarEnabled(false);
        webSettings.setDomStorageEnabled(true);

        webSettings.setLoadWithOverviewMode(true);
        // 默认的是NARROW_COLUMNS
       //webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        // 设置缓存
        //webSettings.setAppCacheEnabled(true);
        // 设置缓存模式,一共有四种模式
        //webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        wv_content.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                //handler.proceed();
                handler.cancel();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {


                if (url != null) {
                    String fun = "javascript:function getClass(parent,sClass) { var aEle=parent.getElementsByTagName('div'); var aResult=[]; var i=0; for(i<0;i<aEle.length;i++) { if(aEle[i].className==sClass) { aResult.push(aEle[i]); } }; return aResult; } ";

                    view.loadUrl(fun);

                    String fun3 = "javascript:function hideOther123() { " +
                            "var oMeta = document.createElement('meta');" +
                            "oMeta.name = 'viewport';" +
                            "oMeta.content='width=device-width, initial-scale=1.0, user-scalable=no'"+
                            "document.getElementsByTagName('head')[0].appendChild(oMeta);"+
                            "}";

                    view.loadUrl(fun3);

                    view.loadUrl("javascript:hideOther123();");

                    String fun2 = "javascript:function hideOther() { " +
                            "var el = getClass(document,'footbanner')[0];el.parentNode.removeChild(el);" +
                            "el = getClass(document,'footbottom')[0];el.parentNode.removeChild(el);" +
                            "el = getClass(document,'sub_col2_r')[0];el.parentNode.removeChild(el);" +
                            "el = getClass(document,'sub_banner')[0];el.parentNode.removeChild(el);" +
                            "el = document.getElementById('header');el.parentNode.removeChild(el);" +
                            "el = document.getElementById('nav');el.parentNode.removeChild(el);" +
                            "document.getElementsByTagName('body')[0].setAttribute('style','width:100%;background:0px;font-size:28px');" +
                            "getClass(document,'all_main')[0].setAttribute('style','background:0px');" +
                            "getClass(document,'all_main_n')[0].setAttribute('style','background:0px');" +
                            "getClass(document,'column')[0].style.width='100%';" +
                            "getClass(document,'sub_col1_l')[0].style.width='100%';" +
                            "getClass(document,'sub_col1')[1].style.width='100%';" +
                            "getClass(document,'sub_col1')[1].style.width='100%';" +
                    "}";

                    view.loadUrl(fun2);

                    view.loadUrl("javascript:hideOther();");
                }

                super.onPageFinished(view, url);
            }

        });

        wv_content.loadUrl(bodyData);
        //wv_content.loadData(getHtmlData("<html><body style='width:420px'>this is page</body></html>"),"text/html","utf-8");

        Log.d("height",wv_content.getContentHeight()+"");
}

    private String getHtmlData(String bodyHTML) {
        String head = "<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                "<style>img{max-width: 100%; width:auto; height:auto;}</style>" +
                "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }



}
